package com.igeek.dao;

import com.igeek.bean.Brand;
import com.igeek.bean.Car;
import com.igeek.bean.Cartype;
import com.igeek.bean.User;
import com.igeek.client.MainSys;
import com.igeek.db.DataDB;



import java.time.LocalDate;
import java.util.*;

public class CarDao {

    List<Car> cars = DataDB.getInstance().cars;
    List<Cartype> cartypes = DataDB.getInstance().cartypes;
    List<Brand> brands = DataDB.getInstance().brands;
    BrandDao brandDao = new BrandDao();
    Scanner scanner = new Scanner(System.in);

    List<Car> comparelist = new ArrayList<>();

    public void showinfo(){

        int index = 0;
        System.out.println(" 品牌   车型   里程数  价格   发布时间");

        for(int i = cars.size();i > 0; i --) {
            System.out.println();
            Car tmpcar = cars.get(i - 1);

            index ++;
            System.out.print(index +". ");
            System.out.print(brandDao.findBrand(tmpcar.getBrandid()) + "   "); // 找品牌名称
            System.out.print(findCartype(tmpcar.getCartypeid()) +"   ");// 找车型名
            System.out.print(tmpcar.getDistance()+"   "); //里程数
            System.out.print(tmpcar.getPrice()+ "   "); //price
            System.out.println(tmpcar.getTime()); // 时间


            }


        }

    public void cartypemanage(){
        System.out.println("1.添加车型\n" +
                "2.删除车型");
        System.out.println("输入1或2");
        int i = scanner.nextInt();
        if(i == 1){
            System.out.println("输入车型名称");
            String cartype = scanner.next();

            int id = 0;
            for (Cartype cartype1 : cartypes){
                if(cartype1.getCartypename().equals(cartype)){
                   id = cartype1.getBrandid();
                   break;
                }
            }
            Cartype cartype2 = new Cartype(cartypes.size() + 1,cartype,id);
            cartypes.add(cartype2);

        }else if(i == 2){
            System.out.println("选择要删除的车型");
            for(int j = 0;j < cartypes.size(); j ++){
                System.out.println((j+1) + ". " );
                System.out.println(cartypes.get(j));
            }
            System.out.println("选择序号");
            int k = scanner.nextInt();
            int id = brandDao.find(cartypes.get(i).getCartypeid());
            cartypes.remove(k);

            for(int j = 0;j < cars.size();j ++){
                if(cars.get(j).getCarid() == id){
                    cars.remove(j);
                }
            }

            List<Car> cars = DataDB.getInstance().currentUser.getCars();
            for(int j = 0;j < cars.size();j ++){
                if(cars.get(j).getCarid() == id){
                    cars.remove(j);
                }
            }

        }

    }


    public void compare(Car car){
        if(DataDB.getInstance().currentUser.getComparelist().size() < 4){
            DataDB.getInstance().currentUser.setComparelist(car);
            MainSys mainSys = new MainSys();
            mainSys.cai(mainSys);
        }else{
            System.out.println("对比车辆已满四辆");
            System.out.println("是否跳转至对比车辆页面,跳转请输入yes");
            String a = scanner.next();
            if(a.equals("yes")){
                int index = 0;
                for(Car car1: comparelist){
                    System.out.println(++ index);
                    System.out.println(car1);
                }
            }
        }

    }
    public void buy(Car car){
            User user = DataDB.currentUser;
            if(car.getPrice() > user.getBalance()){
                user.setBalance(user.getBalance() - car.getPrice());
                System.out.println("交易成功");
                System.out.println("剩余余额"+user.getBalance());
                MainSys mainSys = new MainSys();
                mainSys.showNewInfo(null);
            }else{
                System.out.println("钱不够，请充钱");
                MainSys mainSys = new MainSys();
                mainSys.showNewInfo(null);

            }

    }

    //改3
    public String findCartype(int id){
        String a = "";
        for(Cartype cartype : cartypes){
            if(cartype.getCartypeid() == id){
                a = cartype.getCartypename();
            }
        }
        return a;

    }

    public String findCarBrand(int id){
        String b = "";
        for(Brand brand: DataDB.getInstance().brands){
            if(brand.getBrandid() == id){
                b = brand.getBname();
            }
        }
        return b;

    }


    public List<Car> searchByBrand(){
        List<Car> brandslist = new ArrayList<>();

        for(int i = 0;i < brands.size();i ++){
            int j =  i + 1;
            System.out.print(j+". ");
            System.out.println(brands.get(i).getBname());
        }

        for (Car car : cars){

            for(Brand brand: DataDB.getInstance().brands){
                if(car.getBrandid() == brand.getBrandid()){
                    brandslist.add(car);
                    break;
                }
            }

        }



       // System.out.println(brands.get(1).getCarid());
        return brandslist;

    }

    public ArrayList<Car> searchByPrice(int i ){

        ArrayList<Car> carArrayList = new ArrayList<>();

        double carprice ;
        for(Car car:cars){

            carprice = car.getPrice();
            //System.out.println(carprice);
            //System.out.println("------------------");
            if(i == 1){
                if(carprice >= 0&& carprice <= 50000){
                    carArrayList.add(car);
                }
            }else if(i == 2){
                if(carprice >= 50000 && carprice <= 100000){
                    carArrayList.add(car);
                }
            }else if(i == 3){
                if(carprice >= 100000 && carprice <= 150000){
                    carArrayList.add(car);
                }
            }else {
                carArrayList.add(car);
            }
        }
        return carArrayList;

    }
    public Set<String> findallcartype(int id){

        Set<String> cartypename = new HashSet<>();
        for(Cartype cartype : cartypes){
            if(cartype.getCartypeid() == id){
                cartypename.add(cartype.getCartypename());
            }
        }
        return cartypename;

    }

    public ArrayList<Car> findallcar(String name){

        ArrayList<Car> carArrayList = new ArrayList<>();
        int id = 0 ;
        for(Cartype cartype : cartypes){
            if(name == cartype.getCartypename()){
                id = cartype.getCartypeid();
            }
        }

        for(Car car : cars){
            if(car.getCartypeid() == id){
                carArrayList.add(car);
            }
        }
        return carArrayList;

    }

    public ArrayList<Car> searchByTime(LocalDate localDate1,LocalDate localDate2){
        if(!localDate1.isBefore(localDate2)){
            System.out.println("输入日期前后有错误");
        }
        ArrayList<Car> carArrayList = new ArrayList<>();
        for(Car car : cars){
            if(car.getTime().isAfter(localDate1) && car.getTime().isBefore(localDate2)){
                carArrayList.add(car);
            }
        }
        return carArrayList;
    }

}
